<?php
namespace Modules\Asset\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Asset\Entities\PackageSku;

use Validator;
use ThrowException;
use DB;

class PackageSkuController extends Controller
{

    // 各种状态的数量统计
    public function statusTotal(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();
        $total = [
            'pending' => PackageSku::status('pending')->count(),
            'picked' => PackageSku::status('picked')->count(),
            'return_saled' => PackageSku::status('return_saled')->count(),
            'resaled' => PackageSku::status('resaled')->count(),
            'exchanged' => PackageSku::status('exchanged')->count(),
            'resale_pending' => PackageSku::status('resale_pending')->count(),
            'locked' => PackageSku::status('locked')->count(),
            'all' => PackageSku::status('all')->count(),
        ];

        return [
            'total' => $total
        ];
    }


    // 获取开奖列表
    public function index(Request $req)
    {
        $rule = [
            // 'activity_type' => ['required'],
            // 'activity_id' => ['required'],
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        // $class = @config('activity.map')[$activityType] ?: ThrowException::Conflict('无效的活动类型');

        $where = [];
        if ($req->input('activity_type')) {
            $where['activity_type'] = $req->activity_type;
        }
        if ($req->input('activity_id')) {
            $where['activity_id'] = $req->activity_id;
        }
        if ($req->input('node_type')) {
            $where['node_type'] = $req->node_type;
        }
        if ($req->input('node_id')) {
            $where['node_id'] = $req->node_id;
        }

        $options = $req->all();
        $options = array_merge($options, [
            'where' => $where,
            'with' => ['user', 'pay_order', 'from_package_sku'],
            'only' => [
                'id',
                'uuid',
                'user' => [
                    'id',
                    'uuid',
                    'name',
                    'phone',
                    'headimg'
                ],
                'pay_order' => [
                    'id',
                    'number'
                ],
                'from_package_sku' => [
                    'id',
                    'sid'
                ],
                'open_index',
                'open_indexs',
                'options',
                // 'is_free_order',
                'created_at',
                'source_type',
                'activity_type',
                'activity_type_text',
                'sku_type_text',
                'node_type',
                'node_type_text',
                'node_title',
                'node_money_price',
                'node_score_price',
                'sid',
                'money_price',
                'score_price',
                'return_saled_at',
                'return_money',
                'return_score',
                'return_redpack',
                'title',
                'thumb',
                'total',
                'union_status',
                'union_status_text'
            ]
        ]);

        $list = PackageSku::getList($options);

        return $list->generateListResponse();
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = PackageSku::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }
}
